//
//  PopoverAnimator.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/6/30.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
    
    var isPresented = false
    
    var presentFrame = CGRectZero
    
    //返回负责控制转场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        
        pc.presentFrame = presentFrame
        
        return pc
    }
    
    //提供modal动画对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        
        return self
        
    }
    
    
    //返回提供 dismiss 动画对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        
        return self
    }
    
    
    
    //返回动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        
        
        return isPresented ? 1.2 : 0.25
    }
    
    //转场动画实现函数
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        //        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        //        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        
        if isPresented{
            
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            //将toView添加至容器中
            transitionContext.containerView()?.addSubview(toView)
            
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                
                toView.transform = CGAffineTransformIdentity
                
                }, completion: { (_) -> Void in
                    
                    transitionContext.completeTransition(true)
            })
            
        }else{
            
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                
                    fromView.transform = CGAffineTransformMakeScale(1.0, 0.000001)
                
                }, completion: { (_) -> Void in
                    fromView.removeFromSuperview()
                    
                    transitionContext.completeTransition(true)

            })
           
        }
       
    }

}
